package com.gc.web.controller.business;

import com.gc.business.domain.ElevatorBrand;
import com.gc.business.domain.ElevatorInstall;
import com.gc.business.domain.ElevatorModel;
import com.gc.business.service.IElevatorBrandService;
import com.gc.business.service.IElevatorInstallService;
import com.gc.business.service.IElevatorModelService;
import com.gc.common.annotation.Log;
import com.gc.common.base.Result;
import com.gc.common.constant.BusinessConstants;
import com.gc.common.enums.BusinessType;
import com.gc.common.support.Convert;
import com.gc.common.utils.StringUtils;
import com.gc.common.utils.poi.ExcelUtil;
import com.gc.framework.web.base.BaseController;
import com.gc.system.service.ISysDictDataService;
import com.gc.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 电梯安装 控制层
 *
 * @author liujinteng
 * @date 2020-12-01
 */
@Slf4j
@Controller
@RequestMapping("/business/elevatorInstall")
public class ElevatorInstallController extends BaseController {
    private String prefix = "business/elevatorInstall";

    private final IElevatorInstallService elevatorInstallService;

    private final IElevatorBrandService elevatorBrandService;

    private final IElevatorModelService elevatorModelService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    public ElevatorInstallController(IElevatorInstallService elevatorInstallService,
                                     IElevatorBrandService elevatorBrandService,
                                     IElevatorModelService elevatorModelService) {
        this.elevatorInstallService = elevatorInstallService;
        this.elevatorBrandService = elevatorBrandService;
        this.elevatorModelService = elevatorModelService;
    }

    /**
     * 列表页
     */
    @RequiresPermissions("business:elevatorInstall:view")
    @GetMapping
    public String elevatorInstall() {
        return prefix + "/elevatorInstall";
    }

    /**
     * 新增页
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 编辑页
     */
    @GetMapping("/edit/{installId}")
    public String edit(@PathVariable("installId") Long installId, ModelMap mmap) {
        ElevatorInstall elevatorInstall = elevatorInstallService.getById(installId);
        if (StringUtils.isNotEmpty(elevatorInstall.getElevatorBrand())){
            ElevatorBrand brand = elevatorBrandService.getById(elevatorInstall.getElevatorBrand());
            if (brand == null){
                elevatorInstall.setElevatorBrand("");
            }else {
                elevatorInstall.setElevatorBrand(brand.getBrandName());
            }
        }
        if (StringUtils.isNotEmpty(elevatorInstall.getElevatorModel())){
            ElevatorModel model = elevatorModelService.getById(elevatorInstall.getElevatorModel());
            if (model == null){
                elevatorInstall.setElevatorModel("");
            }else {
                elevatorInstall.setElevatorModel(model.getModelName());
            }
        }
        mmap.put("elevatorInstall", elevatorInstall);
        return prefix + "/edit";
    }

    /**
     * 详情页
     *
     * @param installId
     * @param mmap
     * @return
     */
    @RequiresPermissions("business:elevatorInstall:detail")
    @GetMapping("/detail/{installId}")
    public String detail(@PathVariable("installId") Long installId, ModelMap mmap) {
        ElevatorInstall elevatorInstall = elevatorInstallService.getById(installId);
        if (StringUtils.isNotEmpty(elevatorInstall.getElevatorBrand())) {
            elevatorInstall.setElevatorBrand(elevatorBrandService.getById(elevatorInstall.getElevatorBrand()).getBrandName());
        }
        if (StringUtils.isNotEmpty(elevatorInstall.getElevatorModel())) {
            elevatorInstall.setElevatorModel(elevatorModelService.getById(elevatorInstall.getElevatorModel()).getModelName());
        }
        mmap.put("elevatorInstall", elevatorInstall);
        return prefix + "/detail";
    }

    /**
     * 查询电梯安装列表
     */
    @RequiresPermissions("business:elevatorInstall:list")
    @PostMapping("/list")
    @ResponseBody
    public Result list(ElevatorInstall elevatorInstall) {

        Long attrId = getAttrId();
        if(userService.ifCompanyRole(attrId)){
            elevatorInstall.setCompanyId(attrId);
            startPage();
            List<ElevatorInstall> list = elevatorInstallService.selectElevatorInstallList(elevatorInstall);
            list.forEach(install -> {
                if (StringUtils.isNotEmpty(install.getElevatorBrand())){
                    ElevatorBrand brand = elevatorBrandService.getById(install.getElevatorBrand());
                    if (brand == null){
                        install.setElevatorBrand("");
                    }else {
                        install.setElevatorBrand(brand.getBrandName());
                    }
                }
                if (StringUtils.isNotEmpty(install.getElevatorModel())){
                    ElevatorModel model = elevatorModelService.getById(install.getElevatorModel());
                    if (model == null){
                        install.setElevatorModel("");
                    }else {
                        install.setElevatorModel(model.getModelName());
                    }
                }
            });
            return Result.success(getDataTable(list));
        }else {
            return Result.success(getDataTable(new ArrayList<ElevatorInstall>()));
        }
    }

    /**
     * 新增保存电梯安装
     */
    @RequiresPermissions("business:elevatorInstall:add")
    @Log(title = "电梯安装", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public Result addSave(ElevatorInstall elevatorInstall) {
        elevatorInstallService.save(elevatorInstall);
        return Result.success();
    }

    /**
     * 修改保存电梯安装
     */
    @RequiresPermissions("business:elevatorInstall:edit")
    @Log(title = "电梯安装", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public Result editSave(ElevatorInstall elevatorInstall) {
        elevatorInstallService.updateById(elevatorInstall);
        return Result.success();
    }

    /**
     * 删除电梯安装
     */
    @RequiresPermissions("business:elevatorInstall:remove")
    @Log(title = "电梯安装", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public Result remove(String ids) {
        Long[] longIds = Convert.toLongArray(ids);
        elevatorInstallService.removeByIds(Arrays.asList(longIds));
        return Result.success();
    }

    /**
     * 导出电梯安装列表
     */
    @RequiresPermissions("business:elevatorInstall:export")
    @PostMapping("/export")
    @ResponseBody
    public Result export(ElevatorInstall elevatorInstall) {
        List<ElevatorInstall> list = elevatorInstallService.list(elevatorInstall);
        ExcelUtil<ElevatorInstall> util = new ExcelUtil<>(ElevatorInstall.class);
        util.exportExcel(list, "elevatorInstall");
        return Result.success();
    }
}
